package com.allen.imsystem.common.Const;

import com.allen.imsystem.file.model.MsgFileInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;
import java.io.IOException;

@ConfigurationProperties("path")
public class GlobalConst {

    public static final String ENCODING = "UTF-8";

    public static final Long MAX_NOT_SHOW_TIME_SPACE = 1000*60*5L;

    public static final String DEFAULT_FRIEND_GROUP_NAME = "我的好友";

    public static final Long BLOCK_SIZE = 2 * 1024 * 1024L; // 2MB

    public static final Integer NETTY_PORT = 9898;

    public static final String DEFAULT_GROUP_NAME = "新群聊";


    public static String FILE_BASE_PATH;
    public static String SERVER_IP;

    @Value("${path.file_base_path}")
    public void setBaseUrl(String fileBasePath) {
        FILE_BASE_PATH = fileBasePath;
    }

    @Value("${path.server_ip}")
    public void setStaticPathPrefix(String serverIp) {
        SERVER_IP = serverIp;
    }

    /**
     * websocket事件码
     */
    public static class WsEvent{
        public static final int CLIENT_SEND_MSG = 101;              // 客户端发送信息
        public static final int CLIENT_MSG_ACK = 102;               // 客户端收到信息确认回执
        public static final int CLIENT_NEW_APPLY_NOTIFY_ACK = 104;  // 客户端收到新好友申请通知确认回执
        public static final int CLIENT_NEW_FRIEND_NOTIFY_ACK = 105; // 客户端收到申请被通过获得新好友的通知确认回执

        public static final int SERVER_PUSH_MSG = 201;
        public static final int SERVER_MSG_ACK_SUCCESS = 202;
        public static final int SERVER_MSG_ACK_FAIL = 203;
        public static final int SERVER_PUSH_NEW_APPLY_NOTIFY = 204;
        public static final int SERVER_PUSH_NEW_FRIEND_NOTIFY = 205;
        public static final int SERVER_PUSH_MSG_WITHDRAW = 206;

    }

    /**
     * 路径类
     */
    public static class Path {
        public static String API_SERVER_IP = GlobalConst.SERVER_IP;
        public static String FILE_BASE_PATH = GlobalConst.FILE_BASE_PATH;
        static {
            System.out.printf("API_SERVER_IP: %s, FILE_BASE_PATH: %s\n", API_SERVER_IP, FILE_BASE_PATH);
        }
        public static final String RESOURCES_URL = String.format("http://%s/imsystem/static/", API_SERVER_IP);
        public static final String TALK_WEBSOCKET_URL = String.format("ws://%s/imsystem/websocket", API_SERVER_IP);

        public static final String AVATAR_URL = RESOURCES_URL + "avatar/";
        public static final String MSG_IMG_URL = RESOURCES_URL + "msg_img/";
        public static final String MSG_FILE_URL = RESOURCES_URL + "msg_file/";

        public static final String FILE_ICON_URL = RESOURCES_URL + "msg_file/file_icon.png";
        public static final String IMG_NOT_FOUND = RESOURCES_URL + "msg_img/img_not_found.jpg";

        public static final String DEFAULT_GROUP_AVATAR = "default/default_group_avatar.jpg";

        public static final String BLOCK_IMG_URL = MSG_IMG_URL + "block.png";
        public static final String BLOCK_IMG_MD5 = "2ac7752047c55b39677dfb72f868778c";

        public static final String AVATAR_PATH = FILE_BASE_PATH + "avatar/";
        public static final String MSG_IMG_PATH = FILE_BASE_PATH + "msg_img/";
        public static final String MSG_FILE_PATH = FILE_BASE_PATH + "msg_file/";
        public static final String MSG_RECORD_PATH = FILE_BASE_PATH + "msg_record/";

        static {
            try {
                FileUtils.forceMkdir(new File(AVATAR_PATH));
                FileUtils.forceMkdir(new File(MSG_IMG_PATH));
                FileUtils.forceMkdir(new File(MSG_FILE_PATH));
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }

    /**
     *  用户在线状态
     */
    public static class UserStatus{
        public static final Integer OFFLINE = 0;   // 离线
        public static final Integer ONLINE = 1;    // 在线
        public static final Integer AFK = 2;       // 离开
        public static final Integer HIDE = 3;      // 隐身
    }

    /**
     * 正则表达式
     */
    public static class RegExp {

        public static final String EMAIL = "^[\\w]+@[a-zA-z\\d]+\\.[a-zA-z0-9]+$";
        public static final String PASSWORD = "^*$";
    }

    public static class MQ {
        public static final String EMAIL_MESSAGE_KEY = "EMAIL_MESSAGE_QUEUE";
    }

    /**
     * redis相关的常量
     */
    public static class RedisKey {
        public static final String KEY_USER_STATUS = "user_status";
        public static final String KEY_CHAT_INFO = "chat_info";
        public static final String KEY_RECORD_BEGIN_ID = "msg_record_begin_msg_id";
        public static final String KEY_FRIEND_SET = "user_friend_set_";
        public static final String KET_GROUP_CHAT_MEMBERS = "group_chat_members_";
        public static final String KEY_EMAIL_CODE = "email_code_";
        public static final String KEY_IMAGE_CODE = "image_code_";
        public static final String KEY_TOKEN_BLACKLIST = "token_black_list";
        /**
         * 用户信息缓存的key
         */
        public static final String KEY_USER_INFO = "user_info#";

        /**
         * 群名称
         * gid-uid
         */
        public static final String KEY_PATTERN_GROUP_ALIAS = "group_alias#%s";
    }

    public static class ChatType{
        public static final Integer UN_KNOW = -1;
        public static final Integer PRIVATE_CHAT = 0;
        public static final Integer GROUP_CHAT = 1;
    }

    public static class NotifyType{
        public static final int NEW_FRIEND_NOTIFY  = 1;
        public static final int NEW_APPLY_NOTIFY  = 2;
        public static final int GROUP_NOTIFY = 3;
    }

    public static class MsgType{
        public static final int TEXT = 1;
        public static final int IMAGE = 2;
        public static final int FILE = 3;
        public static final int GROUP_NOTIFY = 4;
    }

    /**
     * id类型
     */
    public static class IdType{
        public static final int UID = 1;
        public static final int GID = 2;
        public static final int CHAT_ID = 3;
        public static final int MSG_ID = 4;
    }

}
